package com.dx.service.impl;

import com.dx.api.RemoteEmailService;
import com.dx.constants.Constants;
import com.dx.context.ContextHolder;
import com.dx.domain.EmailDto;
import com.dx.entity.bo.Employee;
import com.dx.result.R;
import com.dx.result.Result;
import com.dx.service.AcctivateService;
import com.dx.service.EmployeeService;
import com.dx.service.InterfaceOpService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;


@Service
@RequiredArgsConstructor
public class AcctivateServiceImpl implements AcctivateService {
    private final RedisTemplate redisTemplate;
    private final RemoteEmailService remoteEmailService;
    private final EmployeeService employeeService;

    @Override
    public void activation(String email) {


        //生成激活码
        String uuid = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(uuid,email);
        remoteEmailService.sendEmail(new EmailDto(email,"激活链接", Constants.acctivateUrl+uuid,""), "");
    }

    @Override
    public void activationVerify(String uuid) {
        String email = (String)redisTemplate.opsForValue().get(uuid);
        if (email==null){
            throw new RuntimeException("激活码已过期");
        }
        final Employee employee = employeeService.getByEmail(email);
        employeeService.updateStatus(employee.getId(),1);
    }
}
